/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * NewJPanel2.java
 *
 * Created on 09-Sep-2009, 02:18:13
 */

package com.gemom.gui;

import com.gemom.*;
import com.gemom.util.*;
import com.gemom.combinationItems.*;

import java.text.NumberFormat;
import java.util.*;

/**
 *
 * @author Yair D
 */
public class NewJPanel2 extends javax.swing.JPanel {

    /** Creates new form NewJPanel2 */
    public NewJPanel2(int numberitems) {
       
        this.numberitems=numberitems;

        initComponents();

        this.jButton1numBr.setEnabled(false);
         this.jButton1candbids.setEnabled(false);
        jTable1 = new javax.swing.JTable();
        jScrollPane2.setViewportView(jTable1);

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jTextField1numBr = new javax.swing.JTextField();
        jButton1numBr = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        jScrollPane1 = new javax.swing.JScrollPane();
        jPanel2brokers = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jScrollPane3candbids = new javax.swing.JScrollPane();
        jTextArea1candbids = new javax.swing.JTextArea();
        jButton1candbids = new javax.swing.JButton();
        jSeparator2 = new javax.swing.JSeparator();

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Enter number of Brokers"));

        jTextField1numBr.setText("3");

        jButton1numBr.setText("OK");
        jButton1numBr.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1numBrActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jTextField1numBr, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButton1numBr, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(46, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jTextField1numBr, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jButton1numBr))
        );

        jScrollPane1.setBorder(javax.swing.BorderFactory.createTitledBorder("Brokers"));
        jScrollPane1.setViewportView(jPanel2brokers);

        jScrollPane2.setBorder(javax.swing.BorderFactory.createTitledBorder("Items"));

        jScrollPane3candbids.setBorder(javax.swing.BorderFactory.createTitledBorder("Candidate bids (plus dummy bids)"));

        jTextArea1candbids.setColumns(20);
        jTextArea1candbids.setRows(5);
        jTextArea1candbids.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true));
        jScrollPane3candbids.setViewportView(jTextArea1candbids);

        jButton1candbids.setText("List bids");
        jButton1candbids.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1candbidsActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jSeparator2)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 451, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(67, 67, 67)
                        .addComponent(jScrollPane3candbids, javax.swing.GroupLayout.PREFERRED_SIZE, 514, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1candbids))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 1221, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 1164, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(26, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 268, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(25, 25, 25)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 255, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(15, 15, 15)
                        .addComponent(jButton1candbids))
                    .addComponent(jScrollPane3candbids))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1numBrActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1numBrActionPerformed
        // TODO add your handling code here:
        this.numberbrokers=Integer.parseInt(this.jTextField1numBr.getText());
        this.jButton1numBr.setEnabled(false);
         this.jButton1candbids.setEnabled(true);
        methodtobenamed();
    }//GEN-LAST:event_jButton1numBrActionPerformed

    private void jButton1candbidsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1candbidsActionPerformed
        // TODO add your handling code here:

        
        //putting together all bids
        Vector<Bid> bidstoprepro = new Vector();
        for(int i=0;i<arrayOneBrokerPanel.length;i++)
            for(int j=0;j<arrayOneBrokerPanel[i].getlistBids().size();j++)
                bidstoprepro.add(arrayOneBrokerPanel[i].getlistBids().get(j));

        BidPreProcessor bidprepro = new BidPreProcessor( bidstoprepro,itemsArray);


          listFinalBids= bidprepro.getlistFinalBids();

          NumberFormat nf = NumberFormat.getInstance();
        nf.setMaximumFractionDigits(4);

        for(int i=0;i< listFinalBids.size();i++)
            this.jTextArea1candbids.append(
                    "BROKER"+listFinalBids.get(i).getBrokerIdFlag()+
                    "   Bid"+listFinalBids.get(i).getBidID()+": ("
                    +listFinalBids.get(i)+")"+
                    "            Price=  $"+nf.format(listFinalBids.get(i).price)+"\n");
        

        this.jButton1candbids.setEnabled(false);

    }//GEN-LAST:event_jButton1candbidsActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1candbids;
    private javax.swing.JButton jButton1numBr;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2brokers;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3candbids;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JTextArea jTextArea1candbids;
    private javax.swing.JTextField jTextField1numBr;
    // End of variables declaration//GEN-END:variables
   private javax.swing.JTable jTable1;


    int numberbrokers;
    int numberitems;
    CovMatrixData covM;
    private double[][] seriesData;
    private Item[] itemsArray;
    private GBroker[] gBrokersArray;
    private double[] itemsRevenueFactor;
    private oneBrokerPanel[]arrayOneBrokerPanel;
    Vector<Bid> listFinalBids;

    
     Vector<Bid> getlistFBids(){return listFinalBids;}

    public void setSeriesInfo(double[][] seriesData,CovMatrixData covM)
    {   //rows:samples  columns:series
        this.seriesData=seriesData;
        this.covM=covM;
        this.jButton1numBr.setEnabled(true);
    }

    public void methodtobenamed()
    {
        //generates itemsRevenueFactor
        this.itemsRevenueFactor=new double[this.seriesData[0].length];
        double factor=0;
        for(int i=0;i<this.seriesData[0].length;i++)
        {   factor= 1.2 + 0.2 * Math.random();
            this.itemsRevenueFactor[i]=factor;
        }

        //generation of the item objects
        this.itemsArray=new Item[this.seriesData[0].length];
        for(int i=0;i<this.seriesData[0].length;i++)
        {
          this.itemsArray[i]= new Item(i,this.covM.getMeanAndStdDev()[0][i],
                                    this.covM.getMeanAndStdDev()[1][i],this.itemsRevenueFactor[i]);
        }
        
        //generation of the broker guis
        arrayOneBrokerPanel= new oneBrokerPanel[numberbrokers];
         for(int i=0;i<this.numberbrokers;i++)
         {
            arrayOneBrokerPanel[i]= new oneBrokerPanel(i, this.covM,this.itemsArray);
            this.jPanel2brokers.add(arrayOneBrokerPanel[i]);
         }


        //filling the jtable
        NumberFormat nf = NumberFormat.getInstance();
        nf.setMaximumFractionDigits(4);
        Object[][] o= new Object[itemsArray.length][5];
        for(int i=0;i<itemsArray.length;i++)
        {
            o[i][0]= itemsArray[i].getItemID();
            o[i][1]= nf.format(itemsArray[i].getItemRate());
            o[i][2]= nf.format(itemsArray[i].getStdDev());
            o[i][3]= nf.format(itemsArray[i].getRevenueItem());
            o[i][4]= nf.format(itemsArray[i].getRevenueFactor());
        }


          jTable1.setModel(new javax.swing.table.DefaultTableModel(
           o, new String [] {
                "ItemID", "ItemRate","stdDev" , "ItemRevenue","revenueFactor"
            }
        ));


        //Generating the GBrokers array:from the actual gbrokers
          this.gBrokersArray=new GBroker[numberbrokers];
          for(int i=0;i<this.gBrokersArray.length;i++)
              this.gBrokersArray[i]= arrayOneBrokerPanel[i].getBroker();
           



    }


}
